package com.github.binarywang.demo.wechat.controller;


import com.github.binarywang.demo.wechat.utils.JsonUtils;
import org.codehaus.groovy.runtime.StringGroovyMethods;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/ip/")
public class IPTestController {

    @RequestMapping("route/info")
    public String getIpRouteInfo(HttpServletRequest request) {
        HashMap<String, String> resultMap = new HashMap<>();
        String ip = request.getHeader("x-forwarded-for");

        resultMap.put("x-forwarded-for ip", ip);
        // 多次反向代理后会有多个ip值，第一个ip才是真实ip


        ip = request.getHeader("Proxy-Client-IP");
        System.out.println("Proxy-Client-IP ip: " + ip);
        if (ip != null)
            resultMap.put("Proxy-Client-IP ip", ip);

        ip = request.getHeader("WL-Proxy-Client-IP");
        System.out.println("WL-Proxy-Client-IP ip: " + ip);
        if (ip != null)
            resultMap.put("WL-Proxy-Client-IP ip", ip);
        ip = request.getHeader("HTTP_CLIENT_IP");
        System.out.println("HTTP_CLIENT_IP ip: " + ip);
        if (ip != null)
            resultMap.put("HTTP_CLIENT_IP ip", ip);
        ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        System.out.println("HTTP_X_FORWARDED_FOR ip: " + ip);
        if (ip != null)
            resultMap.put("HTTP_X_FORWARDED_FOR ip: ", ip);
        ip = request.getHeader("X-Real-IP");
        System.out.println("X-Real-IP ip: " + ip);
        if (ip != null)
            resultMap.put("X-Real-IP ip", ip);
        ip = request.getRemoteAddr();
        System.out.println("getRemoteAddr ip: " + ip);
        if (ip != null)
            resultMap.put("getRemoteAddr ip", ip);
        String resultJson = JsonUtils.toJson(resultMap);
        System.out.println("ip信息: " + ip);
        return resultJson;


    }
}
